home *** CD-ROM | disk | FTP | other *** search
- Dim MyDB As Database, MyWs As Workspace
- Dim T1, T2 As TableDef
- Dim T1Flds(1 To 2), T2Flds(1 To 3) As Field
- Dim TempFld As Field
- Dim T1Idx, T2Idx As Index
- Dim Rel As Relation
- Dim MyRec As Recordset
- 'Create Database
- Set MyWs = DBEngine.Workspaces(0)
- Set MyDB = MyWs.CreateDatabase("C:\DBNAME.MDB", dbLangGeneral)
- Rem Create first Table, TABLE1
- Set T1 = MyDB.CreateTableDef("TABLE1")
- 'Specify fields for TABLE1
- 'Note the use of the optional parameter 50 for field size
- 'If 50 is omitted, the size will default to 20
- Set T1Flds(1) = T1.CreateField("FIELD1A", dbText, 50)
- Set T1Flds(2) = T1.CreateField("FIELD1B", dbSingle)
- 'Add the New fields to the field list in the Table
- T1.Fields.Append T1Flds(1)
- T1.Fields.Append T1Flds(2)
- 'Specify a primary field for TABLE1
- Set T1Idx = T1.CreateIndex("FIELD1A")
- T1Idx.Primary = True
- T1Idx.Unique = True
- T1Idx.Required = True
- Set T1Flds(1) = T1Idx.CreateField("FIELD1A")
- 'Add this field to the field list of the Index
- T1Idx.Fields.Append T1Flds(1)
- 'Add this Index to the index list of the Table
- T1.Indexes.Append T1Idx
- 'Add the Table to the Database
- MyDB.TableDefs.Append T1
- 'Create TABLE2
- Set T2 = MyDB.CreateTableDef("TABLE2")
- 'Specify fields for TABLE2
- Set T2Flds(1) = T2.CreateField("FIELD2A", dbText, 50)
- Set T2Flds(2) = T2.CreateField("FIELD2B", dbSingle)
- Set T2Flds(3) = T2.CreateField("FIELD2C", dbInteger)
- 'Add the new fields to the field list of the Table
- T2.Fields.Append T2Flds(1)
- T2.Fields.Append T2Flds(2)
- T2.Fields.Append T2Flds(3)
- 'Set the primary field for TABLE2
- Set T2Idx = T2.CreateIndex("FIELD2C")
- T2Idx.Primary = True
- T2Idx.Unique = True
- T2Idx.Required = True
- Set T2Flds(3) = T2Idx.CreateField("FIELD2C")
- 'Add this field to the field list of the Index
- T2Idx.Fields.Append T2Flds(3)
- 'Add this index to the index list of TABLE2
- T2.Indexes.Append T2Idx
- 'Add TABLE2 to the Database
- MyDB.TableDefs.Append T2
- 'Set up the relation between the tables
- Set Rel = MyDB.CreateRelation("foreign", "TABLE1", "TABLE2")
- Rel.Attributes = 0
- 'Mark the primary field in TABLE1
- Set T2Flds(1) = Rel.CreateField("FIELD1A")
- 'Mark the foreign key field in TABLE2
- T2Flds(1).ForeignName = "FIELD2A"
- 'Add the field to the field list of the relation
- Rel.Fields.Append T2Flds(1)
- 'Add the relation to the database
- MyDB.Relations.Append Rel
- 'Add a record to each table
- 'Open a recordset referring to TABLE1
- Set MyRec = T1.OpenRecordset
- 'Create a record
- MyRec.AddNew
- MyRec("FIELD1A") = "alpha"
- MyRec("FIELD1B") = 1997
- 'Update the recordset
- MyRec.Update
- 'Close the recordset referring to TABLE1
- MyRec.Close
- 'Open a recordset referring to TABLE2
- Set MyRec = T2.OpenRecordset
- 'Create a record
- MyRec.AddNew
- MyRec("FIELD2A") = "alpha"
- MyRec("FIELD2B") = 2000
- MyRec("FIELD2C") = 1
- 'Update the recordset
- MyRec.Update
- 'Close the recordset
- MyRec.Close
- 'Close the database
- MyDB.Close
-